#include "widget.h"
#include "./ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_btnGetChars_clicked()
{
    QString str = ui->editStr->text();
    if (str.isEmpty()) return;

    for (qint16 i = 0; i < str.size(); i++) {
        QChar c = str[i];
        char16_t uniCode = c.unicode();

        QString s(c);
        QString info = s + QString::asprintf("\t, Unicode编码 = 0x%X", uniCode);
        ui->plainTextEdit->appendPlainText(info);
    }
}


void Widget::on_btnClear_clicked()
{
    ui->plainTextEdit->clear();
}


void Widget::on_btnCharJudge_clicked()
{
    QString str = ui->editChar->text();
    if (str.isEmpty()) return;

    QChar c = str[0];
    char16_t uniCode = c.unicode();
    QString info = c + QString::asprintf("\t, Unicode编码 = 0x%X", uniCode);
    ui->plainTextEdit->appendPlainText(info);

    ui->chkDigit->setChecked(c.isDigit());
    ui->chkLetter->setChecked(c.isLetter());
    ui->chkLetterOrNumber->setChecked(c.isLetterOrNumber());
    ui->chkUpper->setChecked(c.isUpper());
    ui->chkLower->setChecked(c.isLower());
    ui->chkMark->setChecked(c.isMark());
    ui->chkSpace->setChecked(c.isSpace());
    ui->chkSymbol->setChecked(c.isSymbol());
    ui->chkPunct->setChecked(c.isPunct());
}


void Widget::on_btnConvLatin1_clicked()
{
    // Unicode string
    QString s = "Dimple";
    ui->plainTextEdit->appendPlainText(s);

    // Convert latin1 char to unicode char
//    QChar chP = QChar::fromLatin1('P');
//    QChar chP = QChar('P');
    QChar chP = 'P';

    s[0] = chP;
    ui->plainTextEdit->appendPlainText("\n" + s);
}


void Widget::on_btnConvUTF16_clicked()
{
    // Unicode string
    QString s = "Hello,北京";
    ui->plainTextEdit->appendPlainText(s);

    // Change part of unicode string
#if 1
    QString qd = "青岛";
    s[6] = qd[0];
    s[7] = qd[1];
#else
    s[6] = QChar(0x9752);
    s[7] = QChar(0x5C9B);
#endif
    ui->plainTextEdit->appendPlainText("\n" + s);
}


void Widget::on_btnCompare_clicked()
{
    QString HuStr = "河to湖";
    QChar He = QChar::fromUcs2(HuStr[0].unicode());
    QChar Hu = QChar(HuStr[3].unicode());
    QString str = "他们来自河南或河北";
    const QString originStr = str;

    for (int i = 0; i < str.size(); i++) {
        if (str[i] == He)
            str[i] = Hu;
    }

    QString res = originStr + "\nConvert result: " + str;
    ui->plainTextEdit->appendPlainText(res);
}

